import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:joyagoo/config/app_colors.dart';
import 'joy_home_page.dart';
import 'joy_category_page.dart';
import 'joy_profile_page.dart';
import 'joy_search_trending_page.dart';

class JoyMainTabPage extends StatefulWidget {
  const JoyMainTabPage({super.key});

  @override
  State<JoyMainTabPage> createState() => _JoyMainTabPageState();
}

class _JoyMainTabPageState extends State<JoyMainTabPage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    const JoyHomePage(),
    const JoyCategoryPage(),
    const JoySearchTrendingPage(),
    const JoyProfilePage(),
  ];

  // 自定义底部导航栏项目
  Widget _buildBottomNavItem({
    required IconData icon,
    required IconData activeIcon,
    required String label,
    required int index,
  }) {
    final bool isSelected = _currentIndex == index;
    return GestureDetector(
      onTap: () {
        setState(() {
          _currentIndex = index;
        });
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 4.h), // 从8.h减少到4.h
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: EdgeInsets.symmetric(
                horizontal: isSelected ? 8.w : 6.w, // 从16.w/8.w减少到12.w/6.w
                vertical: 4.h, // 从6.h减少到4.h
              ),
              decoration: BoxDecoration(
                color: isSelected ? AppColors.primaryVariant.withOpacity(0.1) : Colors.transparent,
                borderRadius: BorderRadius.circular(20.r),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Icon(
                    isSelected ? activeIcon : icon,
                    color: isSelected ? AppColors.primaryVariant : Colors.grey[600],
                    size: 18.sp, // 从20.sp减少到18.sp
                  ),
                  if (isSelected) ...[
                     SizedBox(width: 4.w), // 从6.w减少到4.w
                     Text(
                       label,
                       style: TextStyle(
                         color: AppColors.primaryVariant,
                         fontSize: 10.sp, // 从12.sp减少到10.sp
                         fontWeight: FontWeight.w500,
                       ),
                     ),
                   ]
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(
        index: _currentIndex,
        children: _pages,
      ),
      bottomNavigationBar: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 8,
              offset: const Offset(0, -2),
            ),
          ],
        ),
        child: SafeArea(
          child: Container(
            height: 60.h, // 从80.h减少到60.h
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h), // 从8.h减少到4.h
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildBottomNavItem(
                  icon: Icons.home_outlined,
                  activeIcon: Icons.home,
                  label: 'Home',
                  index: 0,
                ),
                _buildBottomNavItem(
                  icon: Icons.grid_view_outlined,
                  activeIcon: Icons.grid_view,
                  label: 'Category',
                  index: 1,
                ),

                _buildBottomNavItem(
                  icon: Icons.search_outlined,
                  activeIcon: Icons.search,
                  label: 'Search',
                  index: 2,
                ),
                _buildBottomNavItem(
                  icon: Icons.person_outline,
                  activeIcon: Icons.person,
                  label: 'My',
                  index: 3,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}